package cn.lili.modules.promotion.mapper;

import cn.lili.modules.promotion.entity.dos.Advertising;
import cn.lili.modules.promotion.entity.dos.AdvertisingSpace;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author:xiangqian
 * @date: 2024/4/17 9:16
 */
public interface AdvertisingMapper extends BaseMapper<Advertising> {

    /**
     * 根据当前时间，更新失效广告
     */
    @Update("UPDATE li_advertising SET status = 2 \n" +
            "WHERE id IN (\n" +
            "    SELECT temp.id \n" +
            "    FROM (\n" +
            "        SELECT id \n" +
            "        FROM li_advertising \n" +
            "        WHERE effective_time_end < NOW() AND status = 0\n" +
            "    ) AS temp\n" +
            ");\n")
    Long updateByEffectiveTimeEnd();

    /**
     * 根据广告位查询广告
     * @param spaceId 广告位ID
     */
    @Select("SELECT\n" +
            "\t* \n" +
            "FROM\n" +
            "\tli_advertising \n" +
            "WHERE\n" +
            "\tspace_id = #{spaceId} \n" +
            "\tAND delete_flag = 0 \n" +
            "\tAND STATUS = 0 \n" +
            "\tAND NOW() > reveal_time_begin \n" +
            "\tAND NOW() < reveal_time_end;")
    List<Advertising> selectBySpaceId(@Param("spaceId") String spaceId);
}
